package com.hh.item.dao;

import com.hh.item.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangfan
 * @date 2020/3/7 13:55
 * @description
 */
@Repository("userDao")
@Mapper
public interface IUserDao extends tk.mybatis.mapper.common.Mapper<User> {

    @Select("select * from user_info where user_name like #{searchText}")
    @Results(id = "userMap", value = {
            @Result(column = "user_id", property = "userId", id = true),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "user_pwd", property = "userPwd"),
            @Result(column = "user_msg_count", property = "msgCount"),
            @Result(column = "user_fans_count", property = "fansCount"),
            @Result(column = "user_follow_count", property = "followCount"),
            @Result(column = "user_sex", property = "userSex"),
            @Result(column = "user_avatar", property = "userAvatar"),
    })
    List<User> searchUser(String searchText);

    @Select("select relation_type from user_relation where user_id=#{userId} and target_id=#{targetId}")
    List<Integer> getRelationStatus(@Param("userId") String userId, @Param("targetId") String targetId);

    @Insert("insert into user_relation(user_id,target_id,relation_type) values (#{userId},#{targetId},1)")
    int follow(@Param("userId") String userId, @Param("targetId") String targetId);

    @Delete("delete from user_relation where user_id=#{userId} and target_id=#{targetId} and relation_type=1")
    int unfollow(@Param("userId") String userId, @Param("targetId") String targetId);

    @Insert("insert into user_relation(user_id,target_id,relation_type) values (#{userId},#{targetId},0)")
    int fan(@Param("userId") String userId, @Param("targetId") String targetId);

    @Delete("delete from user_relation where user_id=#{userId} and target_id=#{targetId} and relation_type=0")
    int unfan(@Param("userId") String userId, @Param("targetId") String targetId);

    @Update("update user_info set user_follow_count=user_follow_count+1 where user_id = #{userId}")
    int addFollow(String userId);

    @Update("update user_info set user_fans_count=user_fans_count+1 where user_id = #{targetId}")
    int addFan(String targetId);

    @Update("update user_info set user_follow_count=user_follow_count-1 where user_id = #{userId}")
    int cancelFollow(String userId);

    @Update("update user_info set user_fans_count=user_fans_count-1 where user_id = #{targetId}")
    int cancelFan(String targetId);

    @Update("update user_info set user_name = #{userName},user_pwd = #{userPwd},user_sex = #{userSex} WHERE user_name = #{oldName}")
    int editMine(@Param("oldName") String oldName, @Param("userName") String userName, @Param("userPwd") String userPwd, @Param("userSex") Integer userSex);

    @Select("SELECT * FROM user_info where user_id = #{userId}")
    @ResultMap("userMap")
    User getMine(Integer userId);

    @Select("SELECT user_name,user_msg_count,user_follow_count,user_fans_count from user_info WHERE user_id IN (SELECT target_id from user_relation WHERE user_id = #{userId} and relation_type = 1)")
    @Results(id = "userMap2", value = {
            @Result(column = "user_name", property = "userName"),
            @Result(column = "user_msg_count", property = "msgCount"),
            @Result(column = "user_fans_count", property = "fansCount"),
            @Result(column = "user_follow_count", property = "followCount")
    })
    List<User> getFollow(Integer userId);

    @Select("SELECT user_name,user_msg_count,user_follow_count,user_fans_count from user_info WHERE user_id IN (SELECT target_id from user_relation WHERE user_id = #{userId} and relation_type = 0)")
    @ResultMap("userMap2")
    List<User> getFans(Integer userId);

    @Select("SELECT user_name,user_id from user_info")
    @Results(value = {
            @Result(column = "user_id", property = "userId", id = true),
            @Result(column = "user_name", property = "userName")
    })
    List<User> searchAllNameAndId();





    /*@Select("select * from user_info")
    @ResultMap("userMap")
    List<User> findAll();

    @Insert("insert into user_info(user_id,user_name,user_pwd,user_msg_count,user_fans_count,user_follow_count,user_sex,user_avatar) values (#{userId},#{userName},#{userPwd},#{msgCount},#{fansCount},#{followCount},#{userSex},#{userAvatar})")
    int save(User user);

    @Update("update user_info set user_name=#{userName},user_pwd=#{userPwd},user_msg_count=#{msgCount},user_fans_count=#{fansCount},user_follow_count=#{followCount},user_sex=#{userSex},user_avatar=#{userAvatar} where user_id = #{userId}")
    int updateUserById(User user);

    @Delete("delete from user_info where user_id = #{id}")
    int deleteUserById(Integer id);

    @Select("select * from user_info where user_id = #{id}")
    @ResultMap("userMap")
    User findUserById(Integer id);*/

    /*@Select("select user_msg_count,user_follow_count,user_fans_count,user_avatar from user_info where user_id=#{userId}")
    @ResultType(HashMap.class)
    Map<String, String> refresh(String userId);

    @Update("update user_info set user_msg_count=user_msg_count+1 where user_id = #{userId}")
    int addMsgCount(String userId);

    @Update("update user_info set user_avatar= #{fullPath} where user_id = #{userId}")
    int uploadProfile(@Param("userId") String userId, @Param("fullPath") String fullPath);*/
}
